/*eslint-disable*/
import axios from 'axios'
import Vue from 'vue'
import { URLS } from './urls'
 import store from '../store/index'

//  http://10.240.73.36:9090  testing
let isProduct = process.env.NODE_ENV === 'production';
// axios.defaults.baseURL = isProduct ? '//vipapi.jxedt.com' : 'http://10.240.73.36:9090' // 'http://vipapitest.58v5.cn';
// formData 形式数据提交

let sender = axios;
// .create({
//     async: true,
//     timeout: 30000,
//     baseURL: isProduct ? 'https://vipapi.jxedt.com' : 'https://vipapitest.58v5.cn', // process.env.hostUrl
//     withCredentials: true,
//     headers: {
//         'X-Requested-With': 'XMLHttpRequest',
//         'Origin': location.href,
//         'Content-MD5': 'f5ec1fa61dbe9c62ddb3d13ef3b3380d',
//         'Content-Type':'1212'
//     }
// });

sender.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8';
// sender.defaults.baseURL =  'http://192.168.100.108:9003';
sender.defaults.baseURL =  'https://www.qhdyywlkj.com/';
// http://10.240.73.36:9090'
// sender.defaults.withCredentials = true;


sender.interceptors.request.use((config) =>{ 
    let header = {  
    }
    return {
        ...{
            ...config,
            headers: header
            // {
            //     'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            //     hpincode: store.state.nativeInfo && store.state.nativeInfo.hpincode
            // }
        },
        params: {
            ...(config.params || {}),
            t: +new Date()
        }
    }
}, function (error) {
    return Promise.reject(error);
});


sender.interceptors.response.use((response) => {
    // if (response.data.code && response.data.code === 40001) {
    //     window.location.href = response.data.redirectUrl
    // }
    if(response.data.code !== 0){
        store.commit('TOAST', response.data && response.data.msg || '服务异常，请稍后再试！');
    }
    return response;
}, function (error) {
    if (error) {
        store.commit('TOAST', error);
    }
    // if (error.response.status === 401) {
    //     Vue.prototype.$confirm('登录状态失效，请刷新页面', '提示', {
    //         confirmButtonText: '确定',
    //         showCancelButton: false,
    //         type: 'warning'
    //     }).then(() => {
    //         location.reload();
    //     }).catch(() => { });
    //     return;
    // }
    return Promise.reject(error);
});


function resolveData(response) {
    return new Promise((resolve, reject) => {
        resolve(response.data);
    }).catch(error => console.log(error));
}

const api = {
    axios(name, config = {data: null}){
        let url = URLS[name] || name;
        let newHeader = {
            ...config.headers,
            // hpincode: store.state.nativeInfo && store.state.nativeInfo.hpincode || 'sss'
        }
        let options = {
            method: config.method || 'get',
            url: url,
            headers: newHeader
        };
        if (options.method === 'post') {
            options.data = config.data || config.params;
        } else if (options.method === 'get') {
            options.params = config.data || config.params;
        }
        return new Promise((resolve, reject) => {
            axios(options).then(response => {
                resolve(response.data);
            }).catch(error => {
                reject(error);
                console.log(error)
            });
        })
    },

    // Get请求
    get(name, param = {}, headers) {
        let newHeader = {
            ...headers,
            // hpincode: store.state.nativeInfo && store.state.nativeInfo.hpincode || 'sss'
        }
        let options = {
            method: 'get',
            url: URLS[name] || name,
            headers: newHeader,
            params: param
        };
        return new Promise((resolve, reject) => {
            axios(options).then(response => {
                resolve(response.data);
            }).catch(error => {
                reject(error);
                console.log(error)
            });
        })

        // let url = URLS[name] || name;
        // if (!url) return Promise.reject('没有填请求的url');
        // const method = 'get';
        // return new Promise((resolve, reject) => {
        //     sender.get(url, { params: param, headers })
        //     .then(response => {
        //         resolve(response.data);
        //     }).catch(error => {
        //         reject(error);
        //         console.log(error)
        //     });
        // })
    },
    // Post请求
    post(name, param, headers) {

        let newHeader = {
            ...headers,
            hpincode: store.state.nativeInfo && store.state.nativeInfo.hpincode || 'sss'
        }
        let url = URLS[name] || name;
        let options = {
            method: 'post',
            url: url,
            headers: newHeader,
            data: param
        };
        return new Promise((resolve, reject) => {
            axios(options).then(response => {
                resolve(response.data);
            }).catch(error => {
                reject(error);
                console.log(error)
            });
        })
        
        // const url = URLS[name] || name;
        // if (!url) return Promise.reject('没有填请求的url');
        // const method = 'post';
        // return new Promise((resolve, reject) => {
        //     sender[method](url, data, {headers})
        //         .then(response => {
        //             resolve(response.data);
        //         }).catch(error => {
        //             reject(error);
        //             console.log(error);
        //         });
        // });
    },
    // push请求
    push(name, params = {}) {
        const url = URLS[name] || name;
        if (!url) return P.reject('没有填请求的url');
        const method = 'push';
        return sender[method](url, params, {
            dataType: 'json'
        }).then(response => {
            return resolveData(response);
        }).catch(error => P.reject(error));
    },
    // delete请求
    delete(name, params = {}) {
        const url = URLS[name] || name;
        if (!url) return P.reject('没有填请求的url');
        const method = 'delete';
        return sender[method](url, params, {
            dataType: 'json'
        }).then(response => {
            return resolveData(response);
        }).catch(error => P.reject(error));
    }
};

// Plugin.install = function (Vue) {
//     Vue.api = api;
//     window.api = api;
//     Object.defineProperties(Vue.prototype, {
//         api: {
//             get() {
//                 return api
//             }
//         }
//     });
// };
// Vue.use(Plugin);
export default api;